package com.vecharm.app.ui.cloudshelf

import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.dongby.sdk.utils.DisplayUtils
import com.ling.sdk.app.App
import com.ling.sdk.utils.dip
import com.vecharm.app.R
import com.vecharm.app.bean.Brand
import com.vecharm.app.bean.GoodsBean
import com.vecharm.app.manager.GoodsManager
import com.vecharm.app.ui.BaseShowActivity
import com.vecharm.app.utils.TOKEN
import com.vecharm.app.utils.url
import kotlinx.android.synthetic.main.activity_cloud_shelf.*

class CloudShelfActivity : BaseShowActivity(), View.OnClickListener {


    override fun getLayoutId() = R.layout.activity_cloud_shelf

    private val list = ArrayList<Brand>()


    private val callBack = {

        clear()
        onInitView()

    }

    override fun onInitView() {
        window.addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)
        super.onInitView()

        clear()
        GoodsBean.getGoods()?.apply { brands?.also { setList(it) } }

        initView()

        GoodsManager.callBack = callBack

        setBackgroundByConfig(background)
    }


    override fun onNetworkChange(isConnected: Boolean, netType: Int) {
        if (isConnected) start()
    }

    private fun start() {
        GoodsManager.start()
    }

    private fun clear() {
        list.clear()
        listView?.adapter?.notifyDataSetChanged()
    }

    override fun onConfigurationChanged(newConfig: Configuration?) {

        clear()
        onInitView()
    }

    private fun initView() {

        var spanCount = 0
        kotlin.run check@{
            var w = 0
            list.forEach {
                spanCount++
                w += GoodsBean.getBrandSize(it).x.dip()
                if (w >= DisplayUtils.getScreenWidth()) return@check
            }
        }
        if (spanCount == 0) spanCount = 3


        val manager = StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL)//定义瀑布流管理器，第一个参数是列数，第二个是方向。


        listView.layoutManager = manager


        listView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            override fun getItemCount() = list.size

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
                return object : RecyclerView.ViewHolder(layoutInflater.inflate(R.layout.item_cloud_shelf, parent, false)) {}
            }

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                val icon = holder.itemView.findViewById<ImageView>(R.id.icon)
                val name = holder.itemView.findViewById<TextView>(R.id.name)
                val brand = list.getOrNull(position) ?: return
                icon.url(App.app, brand.brandImage)
                name.text = brand.brandName
                val size = GoodsBean.getBrandSize(brand)
                icon.layoutParams.apply { this.width = size.x.dip(); this.height = size.y.dip() }
                holder.itemView.tag = brand
                holder.itemView.setOnClickListener(this@CloudShelfActivity)

            }
        }


        listView.adapter?.notifyDataSetChanged()

    }


    private fun setList(data: ArrayList<Brand>) {

        val map = HashMap<String?, ArrayList<Brand>>()

        data.forEach {
            val goodsList = map[it.brandName] ?: ArrayList()
            map[it.brandName] = goodsList
            goodsList.add(it)

        }
        this.list.clear()
        map.filter { !it.value.isEmpty() }.forEach { this.list.add(it.value[0]) }

    }

    override fun onClick(v: View?) {
        val brand = v?.tag as? Brand ?: return
        startActivity(Intent(this, GoodsShelfActivity::class.java).putExtra(TOKEN, brand.id ?: ""))
    }


    private fun setBackgroundByConfig(view: ImageView) {
        GoodsBean.getConfig().cloudShelfBackground?.also {
            view.visibility = View.VISIBLE
            view.url(App.app, it)
            return
        }

        GoodsBean.getConfig().cloudShelfBackgroundColor?.also {
            view.visibility = View.VISIBLE
            try {
                view.setBackgroundColor(Color.parseColor(it))
            } catch (e: Exception) {
            }
            return
        }

    }
}